package com.sl.au.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.KitchenWaste;
import com.sl.au.entity.User;
import com.sl.au.repository.KitchenWasteRepository;
import com.sl.au.service.KitchenWasteService;

@Service("KitchenWasteService")
@Transactional
public class KitchenWasteServiceImpl extends BaseServiceImpl implements KitchenWasteService {

	@Autowired
	KitchenWasteRepository kitchenWasteRepository;

	@Override
	public KitchenWasteRepository getRepository() {
		return this.kitchenWasteRepository;
	}

	@Override
	public List<KitchenWaste> findKitchenWasteByHistory(int start, int length, String key, String orderName,
			String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return kitchenWasteRepository.getKitchenWasteByHistory(key, pageable).getContent();
	}

	@Override
	public List<KitchenWaste> findKitchenWasteByToDay(int start, int length, String key, String orderName, String dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return kitchenWasteRepository.getKitchenWasteByToDay(key, pageable).getContent();
	}

	@Override
	public List<KitchenWaste> getKitchenWastes(int start, int length, String key, String userId, String orderName,
			String dir) {
		Page<KitchenWaste> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = kitchenWasteRepository.getKitchenWastes(key, userId, pageable);
		return pages.getContent();
	}

	@Override
	public List<KitchenWaste> getPage(int start, int length, String userId, String orderName, String dir) {
		Page<KitchenWaste> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = kitchenWasteRepository.getByUserId(userId, pageable);
		return pages.getContent();
	}

	@Override
	public int getCount(String key, String userId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		if (!StringUtils.hasText(key))
			return kitchenWasteRepository.getByUserId(userId, pageable).getContent().size();
		return kitchenWasteRepository.getKitchenWastes(key, userId, pageable).getContent().size();
	}

	@Override
	public List<KitchenWaste> listPartwastes(String bussinessId) {
		Pageable pageable = new PageRequest(0, 10, Sort.Direction.DESC, "createTime");
		return kitchenWasteRepository.getKitchenWasteByHistory(bussinessId, pageable).getContent();
	}

	@Override
	public List<KitchenWaste> getAdminPageByKey(int start, int length, String key) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return kitchenWasteRepository.getAdminPageByKey(key, pageable).getContent();
	}

	@Override
	public List<KitchenWaste> getAdminPageWithoutKey(int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return kitchenWasteRepository.getAdminPageWithoutKey(pageable).getContent();
	}

	@Override
	public int getAdminCount(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		if (!StringUtils.hasText(key)) {
			return kitchenWasteRepository.getAdminPageWithoutKey(pageable).getContent().size();
		}
		return kitchenWasteRepository.getAdminPageByKey(key, pageable).getContent().size();
	}

	@Override
	public List<KitchenWaste> getOtherPageByKey(int start, int length, String key, String userId) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return kitchenWasteRepository.getOtherPageByKey(key, userId, pageable).getContent();
	}

	@Override
	public List<KitchenWaste> getOtherPageWithoutKey(int start, int length, String userId) {

		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return kitchenWasteRepository.getOtherPageWithoutKey(userId, pageable).getContent();
	}

	@Override
	public int getOtherCount(String userId, String key) {

		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		if (!StringUtils.hasText(key)) {
			return kitchenWasteRepository.getOtherPageWithoutKey(userId, pageable).getContent().size();
		}
		return kitchenWasteRepository.getOtherPageByKey(key, userId, pageable).getContent().size();
	}

}
